import Vue from 'vue'
import Vuex from 'vuex'
// 测试数据
//import {teacher_studentGrade, teacherData,} from "@/js/testConfig.mjs";
import {Loading} from 'element-ui';

Vue.use(Vuex)

import axios from "axios";

axios.defaults.baseURL = 'http://localhost:9000/'
axios.defaults.timeout = 5000
axios.interceptors.request.use(config => {
    return config
})

export default new Vuex.Store({
    state: {
        //教师端的学生列表数据数据
        'teacher_studentData': {},
        //教师端的成绩列表数据
        'teacher_gradeData': {},
        //学生端的基础数据
        'student_baseData': {},
        'teacherId': '',
        'studentId': '',

    },
    mutations: {
        //放入学生端的数据
        setStudentBaseData(state, remoteData) {
            state.student_baseData = remoteData
        },
        //放入学生列表数据
        setTeacherStudentData(state, remoteData) {
            state.teacher_studentData = remoteData
        },
        //放入成绩列表数据
        setTeacherGradeData(state, remoteData) {
            state.teacher_gradeData = remoteData
        },
        setTeacherId(state, id) {
            state.teacherId = id
        },
        setStudentId(state,id){
            state.studentId=id
        }

    },
    actions: {
        //{commit}把$store.commit()这个函数拿了出来
        //根据学号来获取基本的数据
        async getStudentBaseData({commit}, sid) {
            console.log('正在从服务器获取相关数据')
            axios
                .get(`student/getBase/${sid}`)
                .then(response => {
                    if (response.data === '') {
                        console.log('数据为空重新获取数据')
                        alert('数据获取失败')
                    } else {
                        console.log('数据获取成功')
                        commit('setStudentBaseData', response.data)
                    }
                })
                .catch(e => {
                    alert('数据获取失败')
                    console.log('数据获取失败')
                    console.log(e)
                })
        },

        async flashDataForTeacher_student({commit}) {
            console.log('获取学生列表的数据')
            const tid = this.state.teacherId
            let loadingInstance = Loading.service({fullscreen: true});
            axios
                .get(`/teacher/getStudentList/${tid}`)
                .then(response => {
                    if (response.data === '') {
                        alert('刷新页面以获取数')
                    } else {
                        console.log('学生列表数据数据获取成功')
                        commit('setTeacherStudentData', response.data)
                        loadingInstance.close()
                    }
                })
                .catch(e => {
                    console.log('学生列表数据获取失败')
                    console.log(e)
                    loadingInstance.close()
                    alert('数据获取失败')
                })
        },

        async flashDataForTeacher_grade({commit}, sid) {
            console.log('获取成绩列表的数据')
            const tid = this.state.teacherId
            let loadingInstance = Loading.service({fullscreen: true});
            axios
                .get(`/teacher/getGradeList/${tid}/${sid}`)
                .then(response => {
                    if (response.data === '') {
                        this.$store.dispatch("flashDataForTeacher_grade", tid)
                    } else {
                        console.log('数据获取成功')
                        commit('setTeacherGradeData', response.data)
                        loadingInstance.close()
                    }
                })
                .catch(e => {
                    loadingInstance.close()
                    console.log('数据获取失败')
                    console.log(e)
                    alert('数据获取失败')
                })
        },
        deleteStudent(id) {
            console.log('正在删除学号为' + id + '的学生')
        },
        addStudent() {
            console.log('正在添加学生')
        },
        deleteGradeByTableId(id) {
            console.log('正在删除表id为' + id + '的成绩')
        },
        addGradeByStudentId(sid) {
            console.log('正在为sid为' + sid + '的学生添加成绩')
        }

    }
    ,
    modules: {}
})
//这是学生端的接受数据格式
//eslint-disable-next-line no-unused-vars
let studentDataFormat = {
    studentId: '',
    subjectList: [''],
    grades: [
        {
            subject: '',
            date: '',
            grade: '',
        }
    ],
    dateList: ['']
}

//学生列表接受的数据格式
//eslint-disable-next-line no-unused-vars
let teacher_studentListFormat = {
    'teacherName': '',
    'studentList': [
        {
            studentId: '',
            name: '',
        },
        // ......
    ]
}
//成绩列表接受的数据格式
//eslint-disable-next-line no-unused-vars
let teacher_gradeListFormat = {
    'studentId': '学号',
    'grades|': [
        {
            'subject': '学科名',
            'date': '日期',
            'grade': '成绩',
            'id|1-30': '成绩表的id'
        },
    ],
}
